package com.roncoo.education.db.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.roncoo.education.api.request.MemberRequest;
import com.roncoo.education.db.dao.MemberDao;
import com.roncoo.education.db.entity.Member;
import com.roncoo.education.util.MyJwtUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MemberService {

    @Resource
    MemberDao memberDao;

    @Resource
    MyJwtUtil jwtUtil;

    public Member getMemberById(Long id){
        return memberDao.findById(id);
    }

    public PageInfo<Member> getMemberPages(MemberRequest req){
        int pageNum = req.getPageNum();
        int pageSize = req.getCount();
        if (req.getPageNum() == null || req.getPageNum() <= 0 || req.getCount() == null) {
            pageNum = 1;
            pageSize = 20;
        }
        Member query = new Member();
        query.setPhone(req.getPhone());
        query.setDelFlag(1);
        PageHelper.startPage(pageNum, pageSize);
        List<Member> newsList = memberDao.findByBean(query);
        return new PageInfo<>(newsList);
    }

    public String login(String phone, String openId) {
        Member query = new Member();
        query.setPhone(phone);
        query.setOpenId(openId);
        query.setDelFlag(1);
        List<Member> members = memberDao.findByBean(query);
        if(CollectionUtil.isNotEmpty(members)) {
            Member admin = members.get(0);
            Member update = new Member();
            update.setId(admin.getId());
            update.setLoginTime(new Date());
            memberDao.updateById(update);
            return jwtUtil.generateToken(admin.getId(),admin.getOpenId());
        }
        return null;
    }

    public Map<String,String> updateMember(MemberRequest req){
        Member member = BeanUtil.copyProperties(req,Member.class);


        Member query = memberDao.findById(req.getId());
        if(query!=null) {
            //已存在的直接算登录上了
            member.setId(query.getId());
            memberDao.updateById(member);
        }
        Map<String,String> ret = new HashMap<>();
        ret.put("isError", "success");
        ret.put("code", "1");
        return ret;
    }

    public Map<String,String> saveMember(MemberRequest req){
        Map<String,String> ret = new HashMap<>();
        if(StrUtil.isBlank(req.getOpenId())){
            ret.put("isError", "openid is not");
            ret.put("code", "99");
            return ret;
        }
        Member member = BeanUtil.copyProperties(req,Member.class);
        Member query = new Member();
        query.setOpenId(req.getOpenId());
        List<Member> list = memberDao.findByBean(query);
        if(CollUtil.isNotEmpty(list)){
            //已存在的直接算登录上了
            Member member2= list.get(0);
            Member update = new Member();
            update.setId(member2.getId());
            update.setLoginTime(new Date());
            memberDao.updateById(update);
            String token =jwtUtil.generateToken(member2.getId(),member2.getOpenId());
            ret.put("isError", "success");
            ret.put("token", token);
            ret.put("code", "1");
            return ret;
        }
        member.setDelFlag(1);
        member.setCreateTime(new Date());
        memberDao.insert(member);
        Long pid = member.getId();
        String token =jwtUtil.generateToken(pid,member.getOpenId());
        ret.put("isError", "success");
        ret.put("token", token);
        ret.put("code", "1");
        return ret;
    }
}
